/*****************************************************************************
* File Name: keyboard.h
*
* Version: 1.0
*
* Description:
* This file contains API for reading the button status present in a matrix format
*
* Note:
* Define DISABLE_KEYBOARD in platform.h to disable keyboard module
* Define KEYBOARD_MULTIMEDIA_SUPPORT in platform.h to enable multimedia support in keyboard module
* Define ENABLE_IR_CODES in platform.h to enable IR support in keyboard module
*
* Owner: MRAO
*
* Related Document:
* cy_pin component datasheet
*
* Hardware Dependency:
* Keypad having 3x3 Matrix.
*
* Code Tested With:
* 1. PSoC Creator 3.1 
* 2. ARM GCC 4.8.4
******************************************************************************
* Copyright (2015), Cypress Semiconductor Corporation.
******************************************************************************
* This software is owned by Cypress Semiconductor Corporation (Cypress) and is
* protected by and subject to worldwide patent protection (United States and
* foreign), United States copyright laws and international treaty provisions.
* Cypress hereby grants to licensee a personal, non-exclusive, non-transferable
* license to copy, use, modify, create derivative works of, and compile the
* Cypress Source Code and derivative works for the sole purpose of creating
* custom software in support of licensee product to be used only in conjunction
* with a Cypress integrated circuit as specified in the applicable agreement.
* Any reproduction, modification, translation, compilation, or representation of
* this software except as specified above is prohibited without the express
* written permission of Cypress.
*
* Disclaimer: CYPRESS MAKES NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, WITH
* REGARD TO THIS MATERIAL, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 
* Cypress reserves the right to make changes without further notice to the
* materials described herein. Cypress does not assume any liability arising out
* of the application or use of any product or circuit described herein. Cypress
* does not authorize its products for use as critical components in life-support
* systems where a malfunction or failure may reasonably be expected to result in
* significant injury to the user. The inclusion of Cypress' product in a life-
* support systems application implies that the manufacturer assumes all risk of
* such use and in doing so indemnifies Cypress against all charges. Use may be
* limited by and subject to the applicable Cypress software license agreement.
*****************************************************************************/
#ifndef _KEYBOARD_H_
#define _KEYBOARD_H_

#include "platform.h"
#include "device.h"
#include "kdef.h"

#ifndef DISABLE_KEYBOARD

#define MENU_KEY																(0x01)
#define RETURN_KEY															(0x02)
#define OK_KEY																	(0x04)
/* Number of columns and Rows */
#define COLUMNS                                 (3u)
#define ROWS                                    (4u)

#define NO_COLUMN_PORTS                         (1u)
/* Keyboard Debounce Count in KEY_DOWN_DELAY_SAMPLE_PERIOD periods */
#define KEYBOARD_DEBOUNCE_COUNT                 (2u)
#define KEY_QUEUE_LEN                           (18u)

/* Time after which the same code need to be repeated in msec */
#define KEYBOARD_KEEP_ALIVE_TIMEOUT             (108u)

/* For Debounce */
#define INVALID_INDEX                           (0xFF)
#define INVALID_IR_CODE                         (0xFF)
#define INVALID                                 (0xFF)

/*Type of keys in keyboard*/
typedef enum _KeyType_
{
    STD_KEY                 = 0x01,
    MULT_KEY                = 0x02,
    PWR_KEY                 = 0x03,
    DEVICE_4                = 0x04,
    MOUSE_KEY               = 0x05,
    OTHER_KEY               = 0x06,
    MODIFIER_KEY            = 0x80,
    KEY_TYPE_MASK           = 0x7F,
    NO_KEY_TYPE             = 0x7F
}KeyType;

/*keyboard key structure*/
typedef struct _Key_
{
    KeyType  keyType;
    uint8    scanCode;
}Key;

/*Key state value*/
typedef enum _KeyState_
{
    KEY_STATE_DOWN  = 0x1,
    KEY_STATE_UP    = 0x0
}KeyState;

/*Key element structure*/
typedef struct _KeyElement_
{
    KeyState    keyState;
    uint8       index;
    uint8       debounceValue;
    uint8       isDataSent;
}KeyElement;

#define KEY_NUM_LOCK                          NOKEY /*Standard num  lock key*/
#define KEY_CAP_LOCK                          NOKEY /*Standard caps lock key*/
#define KEY_SCROLL_LOCK                       NOKEY /*Standard scroll lock key*/

#define KeyboardDelay()                       CyDelayUs(20)

#define CAPS_LOCK_LED_ON
#define CAPS_LOCK_LED_OFF

#define NUM_LOCK_LED_ON                 
#define NUM_LOCK_LED_OFF

#define SCROLL_LOCK_LED_ON                 
#define SCROLL_LOCK_LED_OFF
#define REPEATCODE_ENABLE                       1u
#define REPEATCODE_DISABLE                      0u
#define Keyboard_IsPairButtonPressed()          (isPairButtonPressed) /*To check pair button status*/
#define Keyboard_IsButtonStatusChanged()        (isButtonStatusChanged)
#define Keyboard_IsPairButtonDown()             (isPairButtonDown)
#if (NO_COLUMN_PORTS > 1)
#define Keyboard_Columns_Interrupt_Enable()     Keyboard_Columns_1_Interrupt_Enable(); \
                                                Keyboard_Columns_2_Interrupt_Enable(); \
                                                Keyboard_Columns_3_Interrupt_Enable()

#define Keyboard_Columns_Interrupt_Disable()    Keyboard_Columns_1_Interrupt_Disable(); \
                                                Keyboard_Columns_2_Interrupt_Disable(); \
                                                Keyboard_Columns_3_Interrupt_Disable()

#define Keyboard_Columns_ClearInterrupt()       Keyboard_Columns_1_ClearInterrupt(); \
                                                Keyboard_Columns_2_ClearInterrupt(); \
                                                Keyboard_Columns_3_ClearInterrupt()
#endif /* (NO_COLUMN_PORTS > 1) */

//// key sequences add zozo
#define KEYS_PAIR_CNT														2
#define MENU_INDEX															6
#define RETURN_INDEX														4


extern bool isPairButtonPressed;
extern bool isButtonStatusChanged;
extern bool isPairButtonDown;
/*****************************************************************************
* Function Name: Keyboard_Init()
******************************************************************************
* Summary:
* Initilize the keyboard module 
*
* Parameters:
* None
*
* Return:
* None
 ******************************************************************************/
extern void Keyboard_Init(void);

/*****************************************************************************
* Function Name: Keyboard_Poll()
******************************************************************************
* Summary:
* Keyboard module will be polled to detect if any button is pressed
*
* Parameters:
* None
*
* Return:
* None
 ******************************************************************************/
extern void Keyboard_Poll(void);

/*****************************************************************************
* Function Name: Keyboard_Get_Report()
******************************************************************************
* Summary:
* Keyboard module provides the Report data for transmitting 
*
* Parameters:
* report - structure in which data to be filled
*
* Return:
* ReportType - Type of report that the data is filled
 ******************************************************************************/
extern ReportType Keyboard_Get_Report(Report *report);

/*****************************************************************************
* Function Name: Keyboard_Set_State()
******************************************************************************
* Summary:
* This functions is used to set the device state and make corresponding configuration
* changes in keyboard module
*
* Parameters:
* state - state of the device
*
* Return:
* None
 ******************************************************************************/
extern void Keyboard_Set_State(Device_State state);

/*****************************************************************************
* Function Name: Keyboard_IsActive()
******************************************************************************
* Summary:
* Checks any key is pressed in keyboard matrix
*
* Parameters:
* None
*
* Return:
* None
 ******************************************************************************/
extern bool Keyboard_IsActive(void);

/*****************************************************************************
* Function Name: Keyboard_Stop()
******************************************************************************
* Summary:
* This functions is used to stop keyboard module
*
* Parameters:
* None
*
* Return:
* None
 ******************************************************************************/
extern void Keyboard_Stop(void);

#ifdef BACK_CHANNEL_SUPPORT
/*****************************************************************************
* Function Name: Keyboard_Stop()
******************************************************************************
* Summary:
* This functions is used to update the status of the led depending upon the 
* back channel data
*
* Parameters:
* back_channel_data - back channel data received 
*
* Return:
* None
 ******************************************************************************/
extern void Keyboard_Update_BackChannel_Data(uint8 back_channel_data);
#endif /* BACK_CHANNEL_SUPPORT */
#else
#define Keyboard_IsPairButtonPressed()           false
#define Keyboard_IsButtonStatusChanged()         false
#define Keyboard_Init()
#define Keyboard_Poll()
#define Keyboard_Get_Report(report)               NO_REPORT
#define Keyboard_IsActive()                       false
#define Keyboard_Set_State(state)
#define Keyboard_Stop()
#define Keyboard_Update_BackChannel_Data(back_channel_data)
#define Keyboard_IsPairButtonDown()           	false

#endif /* DISABLE_KEYBOARD */

#endif /* _KEYBOARD_H_ */
